package com.meiqiongproperty.yiguanjia.ui.photo.view;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.ui.NavigationLayout.NavigationLayout;
import com.meiqiongproperty.yiguanjia.ui.photo.util.MediaUtils;
import com.meiqiongproperty.yiguanjia.ui.photo.widget.PhotoViewPager;
import com.readystatesoftware.systembartint.SystemBarTintManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Arlen on 2016/8/8 17:12.
 */
public class ImageLookActivity extends Activity implements PhotoViewPager.Callback {
    private int mPosition;
    private int mMode;
    private MediaUtils.ImageProperty mCurrentImageProperty;
    private ArrayList<MediaUtils.ImageProperty> mList;
    private List<String> mListUrl;


    private PhotoViewPager mPhotoViewPager;
    private NavigationLayout navigationLayout;

    public static final int REQUEST_CODE = 0x0121;
    public static final int MODE_DELETE = 0x001;
    public static final int MODE_LOOK = 0x002;

    private static final String KEY_POSITION = "position";
    private static final String KEY_MODE = "mode";
    public static final String KEY_IMAGE_PROPERTY = "image_property";

    public static void startForResultImageLookActivity(Activity activity, int mode, int position, ArrayList<MediaUtils.ImageProperty> list) {
        Intent intent = new Intent(activity,  ImageLookActivity.class);
        intent.putExtra(KEY_POSITION, position);
        intent.putExtra(KEY_MODE, mode);
        intent.putExtra(KEY_IMAGE_PROPERTY, list);
        activity.startActivityForResult(intent, REQUEST_CODE);
    }

    public static void startImageLookActivity(Context activity, int mode, int position, String[] imageUrl) {
        Intent intent = new Intent(activity,  ImageLookActivity.class);
        intent.putExtra(KEY_POSITION, position);
        intent.putExtra(KEY_MODE, mode);
        ArrayList<MediaUtils.ImageProperty> list = new ArrayList<>();
        for (String url : imageUrl) {
            MediaUtils.ImageProperty imageProperty = new MediaUtils.ImageProperty("", "", "", "", "", "", url, 0, true);
            list.add(imageProperty);
        }
        intent.putExtra(KEY_IMAGE_PROPERTY, list);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_image);
        mPosition = getIntent().getIntExtra(KEY_POSITION, 0);
        mMode = getIntent().getIntExtra(KEY_MODE, MODE_LOOK);
        mList = (ArrayList<MediaUtils.ImageProperty>) getIntent().getSerializableExtra(KEY_IMAGE_PROPERTY);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus();
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.color.app_basic_color);//通知栏所需颜色
        initView();
        initData();
    }


    @TargetApi(19)
    protected void setTranslucentStatus() {
        Window window = getWindow();
        // Translucent status bar
        window.setFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        // Translucent navigation bar
//        window.setFlags(
//                WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
//                WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }

    private void initData() {
        if (mList == null)
            return;
        navigationLayout.setTitle((mPosition + 1) + "/" + mList.size());
        mCurrentImageProperty = mList.get(mPosition);
        mPhotoViewPager.show("照片列表", mList, mPosition);
    }

    private void initView() {
        navigationLayout = (NavigationLayout) findViewById(R.id.navigation);
        navigationLayout.setTitle("1/1");
        mPhotoViewPager = (PhotoViewPager) findViewById(R.id.photo_view_pager);

        navigationLayout.setrRightImg(R.mipmap.icon_delete);
        navigationLayout.hideRight(mMode == MODE_DELETE ? false : true);
//        mIvDelete.setVisibility(? View.VISIBLE : View.GONE);
        navigationLayout.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        navigationLayout.setRightClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCurrentImageProperty != null) {
                    mPhotoViewPager.removePage(mCurrentImageProperty);
                }
            }
        });
        mPhotoViewPager.setCallback(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onBackPressed() {
        if (mMode == MODE_LOOK) {
            super.onBackPressed();
        } else {
            Intent intent = new Intent();
            intent.putExtra(KEY_IMAGE_PROPERTY, mList);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    @Override
    public void onPageSelected(int position, MediaUtils.ImageProperty imgPro) {
        mCurrentImageProperty = mList.get(position);
        navigationLayout.setTitle((position + 1) + "/" + mList.size());
    }

    @Override
    public void onPageScrolled(int position, MediaUtils.ImageProperty imgPro) {

    }

    @Override
    public void onPageClicked(MediaUtils.ImageProperty imgPro) {

    }

    @Override
    public void onDismissed() {
        onBackPressed();
    }

    @Override
    public void onPreShow(String title, int totalSize, int beginPos) {

    }
}
